﻿using System;
using System.Collections;
using System.Collections.Generic;
using IDAL;
using Model;

namespace BLL
{
    public class NoticeService
    {
        private static readonly INoticeDAL dal = DALFactory.DataAccess.createNoticeDAL();

        //发布通知 三种类型
        public Hashtable publishNotice(int whos, string content)
        {
            IList<User> userList = new List<User>();
            AdminService adminService = new AdminService();
            switch (whos)
            {
                case 0: /* 全体成员 */
                    userList = adminService.getAllUser();
                    break;
                case 1: /* 普通成员 */
                    userList = adminService.getNormalUser();
                    break;
                case 2: /* 管理组成员 */
                    userList = adminService.getAdminUser();
                    break;
                default:
                    userList = null;
                    break;
            }
            Notice notice = null;
            foreach (User u in userList)
            {
                notice = new Notice()
                {
                    toWho = u,
                    content = content,
                    noticeTime = DateTime.Now,
                    status = 0 /* 未读 */
                };
                dal.saveNotice(notice);
            }
            Hashtable ht = new Hashtable();
            ht.Add("result", "1");
            return ht;
        }

        //给用户发送通知
        public Hashtable publishNotice(User u, string content)
        {
            Notice notice = new Notice()
            {
                toWho = u,
                content = content,
                noticeTime = DateTime.Now,
                status = 0
            };
            dal.saveNotice(notice);
            Hashtable ht = new Hashtable();
            ht.Add("result", "1");
            return ht;
        }

        //查看user全部通知
        public IList<Notice> getUserAllNotice(int userId)
        {
            UserService us = new UserService();
            User u = us.getUserById(userId);
            if (u == null)
                return null;
            return dal.getNotice(u);
        }

        //查看user未读通知
        public IList<Notice> getUserNotReadNotice(int userId)
        {
            UserService us = new UserService();
            User u = us.getUserById(userId);
            if (u == null)
                return null;
            return dal.getNotice(u, 0);
        }

        //根据id查看通知
        public Notice getNoticeById(int id)
        {
            return dal.getNotice(id);
        }

    }
}
